/*
 * Created on 29.08.2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package at.fhv.dcs.aw.j2me.lcdui.lowlevel.sort;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class SortingMIDlet extends MIDlet implements CommandListener {

    protected Display m_display;
    protected SortingCanvas m_sortingCanvas;
    protected Command m_exitCmd;
    
    public SortingMIDlet() {
        m_sortingCanvas = new SortingCanvas(); //initialization in startApp causes NullPointerException?!
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        m_sortingCanvas.stop();
    }

    protected void pauseApp() {
        m_sortingCanvas.stop();
        
        Alert alert = new Alert("Continue...");
        alert.setTimeout(1000);
        m_display.setCurrent(alert);
    }

    protected void startApp() throws MIDletStateChangeException {
        if (m_display == null) {
            m_display = Display.getDisplay(this);
                                  
            m_display.setCurrent(m_sortingCanvas);                        
            m_sortingCanvas.setCommandListener(this);
            
            m_exitCmd = new Command("Exit", Command.EXIT, 1);
            m_sortingCanvas.addCommand(m_exitCmd);
        }
        
        m_sortingCanvas.start();
    }

    public void commandAction(Command cmd, Displayable d) {
        if (cmd == m_exitCmd) {
            try {
                destroyApp(false);
            } catch (MIDletStateChangeException e) {
                e.printStackTrace();
            }
            notifyDestroyed();
        }
    }
}
